﻿namespace Sidvall.WorkItemManager.UI.Entities
{
	public sealed class TreeItem : global::Sidvall.WorkItemManager.UI.Entities.Core.BusinessBase
	{
		#region Public Members

		#region ServerManager

		private ServerManager _ServerManager;
		public ServerManager ServerManager
		{
			get
			{
				return _ServerManager;
			}
			set
			{
				_ServerManager = value;
				OnPropertyChanged("ServerManager");
				OnPropertyChanged("Conditions");
				OnPropertyChanged("RetryPolicies");
				OnPropertyChanged("WorkItemTypes");
			}
		}

		#endregion
		#region Conditions

		public WorkConditionCollection Conditions
		{
			get
			{
				return this.ServerManager.Conditions;
			}
		}

		#endregion
		#region RetryPolicies

		public WorkRetryPolicyCollection RetryPolicies
		{
			get
			{
				return this.ServerManager.RetryPolicies;
			}
		}

		#endregion
		#region WorkItemTypes

		public WorkItemTypeCollection WorkItemTypes
		{
			get
			{
				return this.ServerManager.WorkItemTypes;
			}
		}

		#endregion

		#region Name

		private string _Name = string.Empty;
		public string Name
		{
			get
			{
				return _Name;
			}
			set
			{
				_Name = value;
				OnPropertyChanged("Name");
			}
		}

		#endregion
		#region FullName

		private string _FullName = string.Empty;
		public string FullName
		{
			get
			{
				return _FullName;
			}
			set
			{
				_FullName = value;
				OnPropertyChanged("FullName");
			}
		}

		#endregion
		#region ServerConnectionId

		private int _ServerConnectionId;
		public int ServerConnectionId
		{
			get
			{
				return _ServerConnectionId;
			}
			set
			{
				_ServerConnectionId = value;
				OnPropertyChanged("ServerConnectionId");
			}
		}

		#endregion
		#region WorkerId

		private System.Nullable<long> _WorkerId;
		public System.Nullable<long> WorkerId
		{
			get
			{
				return _WorkerId;
			}
			set
			{
				_WorkerId = value;
				OnPropertyChanged("WorkerId");
			}
		}

		#endregion
		#region ItemId

		private System.Nullable<long> _ItemId = null;
		public System.Nullable<long> ItemId
		{
			get
			{
				return _ItemId;
			}
			set
			{
				_ItemId = value;
				OnPropertyChanged("ItemId");
			}
		}

		#endregion
		#region SortOrder

		private string _SortOrder = string.Empty;
		public string SortOrder
		{
			get
			{
				return _SortOrder;
			}
			set
			{
				_SortOrder = value;
				OnPropertyChanged("SortOrder");
			}
		}

		#endregion
		#region HasPoorRating

		private bool _HasPoorRating = false;
		public bool HasPoorRating
		{
			get
			{
				return _HasPoorRating;
			}
			set
			{
				_HasPoorRating = value;
				OnPropertyChanged("HasPoorRating");
			}
		}

		#endregion
		#region IsCanceled

		private bool _IsCanceled = false;
		public bool IsCanceled
		{
			get
			{
				return _IsCanceled;
			}
			set
			{
				_IsCanceled = value;
				OnPropertyChanged("IsCanceled");
				OnPropertyChanged("IsEnabledAndCanceled");
			}
		}

		#endregion
		#region IsEnabled

		private bool _IsEnabled = false;
		public bool IsEnabled
		{
			get
			{
				return _IsEnabled;
			}
			set
			{
				_IsEnabled = value;
				OnPropertyChanged("IsEnabled");
				OnPropertyChanged("IsEnabledAndCanceled");
			}
		}

		#endregion
		#region IsEnabledAndCanceled

		public bool IsEnabledAndCanceled
		{
			get
			{
				if ((this.IsEnabled) && (this.IsCanceled))
					return true;
				return false;
			}
		}

		#endregion
		#region IsExecuting

		private bool _IsExecuting = false;
		public bool IsExecuting
		{
			get
			{
				return _IsExecuting;
			}
			set
			{
				_IsExecuting = value;
				OnPropertyChanged("IsExecuting");
			}
		}

		#endregion
		#region IsForceExecution

		private bool _IsForceExecution = false;
		public bool IsForceExecution
		{
			get
			{
				return _IsForceExecution;
			}
			set
			{
				_IsForceExecution = value;
				OnPropertyChanged("IsForceExecution");
			}
		}

		#endregion
		#region IsServer

		private bool _IsServer = false;
		public bool IsServer
		{
			get
			{
				return _IsServer;
			}
			set
			{
				_IsServer = value;
				OnPropertyChanged("IsServer");
			}
		}

		#endregion
		#region IsWorker

		private bool _IsWorker = false;
		public bool IsWorker
		{
			get
			{
				return _IsWorker;
			}
			set
			{
				_IsWorker = value;
				OnPropertyChanged("IsWorker");
			}
		}

		#endregion
		#region IsItem

		private bool _IsItem = false;
		public bool IsItem
		{
			get
			{
				return _IsItem;
			}
			set
			{
				_IsItem = value;
				OnPropertyChanged("IsItem");
			}
		}

		#endregion
		#region ChildCount

		private int _ChildCount;
		public int ChildCount
		{
			get
			{
				return _ChildCount;
			}
			set
			{
				_ChildCount = value;
				OnPropertyChanged("ChildCount");
			}
		}

		#endregion
		#region LastResultIsOk

		private bool _LastResultIsOk = false;
		public bool LastResultIsOk
		{
			get
			{
				return _LastResultIsOk;
			}
			set
			{
				_LastResultIsOk = value;
				OnPropertyChanged("LastResultIsOk");
			}
		}

		#endregion
		#region LastResultIsCanceled

		private bool _LastResultIsCanceled = false;
		public bool LastResultIsCanceled
		{
			get
			{
				return _LastResultIsCanceled;
			}
			set
			{
				_LastResultIsCanceled = value;
				OnPropertyChanged("LastResultIsCanceled");
			}
		}

		#endregion
		#region LastResultIsFailed

		private bool _LastResultIsFailed = false;
		public bool LastResultIsFailed
		{
			get
			{
				return _LastResultIsFailed;
			}
			set
			{
				_LastResultIsFailed = value;
				OnPropertyChanged("LastResultIsFailed");
			}
		}

		#endregion
		#region TreeItemId

		public string TreeItemId
		{
			get
			{
				return GetTreeItemId(this.ServerConnectionId, this.WorkerId, this.ItemId);
			}
		}

		#endregion
		#region ItemTooltip

		private string _ItemTooltip;
		public string ItemTooltip
		{
			get
			{
				return _ItemTooltip;
			}
			set
			{
				if (string.IsNullOrWhiteSpace(value))
					value = null;
				_ItemTooltip = value;
				OnPropertyChanged("ItemTooltip");
			}
		}

		#endregion
		#region ResultTooltip

		private string _ResultTooltip;
		public string ResultTooltip
		{
			get
			{
				return _ResultTooltip;
			}
			set
			{
				if (string.IsNullOrWhiteSpace(value))
					value = null;
				_ResultTooltip = value;
				OnPropertyChanged("ResultTooltip");
			}
		}

		#endregion
		#region StatusTooltip

		private string _StatusTooltip;
		public string StatusTooltip
		{
			get
			{
				return _StatusTooltip;
			}
			set
			{
				if (string.IsNullOrWhiteSpace(value))
					value = null;
				_StatusTooltip = value;
				OnPropertyChanged("StatusTooltip");
			}
		}

		#endregion

		#region GetSortOrder

		public static string GetSortOrder(int serverConnectionId)
		{
			return GetSortOrder(serverConnectionId, null, null);
		}
		public static string GetSortOrder(int serverConnectionId, string name)
		{
			string key;

			key = serverConnectionId.ToString().PadLeft(7, '0');
			if (name != null)
				key += "." + name;
			return key;
		}
		public static string GetSortOrder(int serverConnectionId, long? workerId)
		{
			return GetSortOrder(serverConnectionId, workerId, null);
		}
		public static string GetSortOrder(int serverConnectionId, long? workerId, int? executionOrder)
		{
			string key;

			key = serverConnectionId.ToString().PadLeft(7, '0');
			if (workerId != null)
				key += "." + workerId.ToString().PadLeft(7, '0');
			if (executionOrder != null)
				key += "." + executionOrder.ToString().PadLeft(7, '0');
			return key;
		}

		#endregion
		#region GetTreeItemId

		public static string GetTreeItemId(int serverConnectionId)
		{
			return GetTreeItemId(serverConnectionId, null, null);
		}
		public static string GetTreeItemId(int serverConnectionId, long? workerId)
		{
			return GetTreeItemId(serverConnectionId, workerId, null);
		}
		public static string GetTreeItemId(int serverConnectionId, long? workerId, long? itemId)
		{
			string key;

			key = serverConnectionId.ToString();
			if (workerId != null)
				key += "." + workerId.ToString();
			if (itemId != null)
				key += "." + itemId.ToString();
			return key;
		}

		#endregion
		#region ToString

		public override string ToString()
		{
			if (this.IsServer)
				return this.Name;
			if (this.IsWorker)
				return this.ServerConnectionId.ToString() + "." + this.Name;
			return this.ServerConnectionId.ToString() + "." + this.WorkerId.ToString() + "." + this.Name;
		}

		#endregion

		#endregion
		#region Constructors

		public TreeItem()
		{
		}

		public TreeItem(Sidvall.Data.DataRowState? dataRowState)
			: base(dataRowState)
		{
		}

		#endregion
	}
}